package com.bjsxt.homework6;

class Printer implements Runnable {

    private final char letter;
    private final int targetState;

    private final Object lock;

    public Printer(char letter, int targetState, Object lock) {
        this.letter = letter;
        this.targetState = targetState;
        this.lock = lock;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (lock) {
                while ((PrintABC.state % 3) != targetState) {
                    try {
                        lock.wait(); // 如果不是自己的状态，就等待
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt(); // 保留中断状态
                        return;
                    }
                }
                System.out.print(letter); // 打印字母
                (PrintABC.state)++; // 更新状态
                lock.notifyAll(); // 唤醒所有等待中的线程
            }
        }
    }
}